package com.leyou.item.controller;


import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/spec")
@RestController
public class SpecController {

    private SpecGroupService specGroupService;

    private SpecParamService specParamService;

    public SpecController(SpecGroupService specGroupService, SpecParamService specParamService) {
        this.specGroupService = specGroupService;
        this.specParamService = specParamService;
    }

    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupListByCategoryId(@RequestParam("id") Long id) {
        return ResponseEntity.ok(specGroupService.queryGroupListByCategoryId(id));
    }

    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParams(
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "groupId", required = false) Long groupId,
            @RequestParam(value = "searching", required = false) Boolean searching
    ) {
        return ResponseEntity.ok(specParamService.querySpecParams(categoryId, groupId, searching));
    }

    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querySpecListByCategoryId(@RequestParam("id") Long id) {
        return ResponseEntity.ok(specGroupService.querySpecListByCategoryId(id));
    }

    @PostMapping("/param")
    public ResponseEntity<Void> addSpecParams(@RequestBody SpecParamDTO specParamDTO) {
        specParamService.addSpecParams(specParamDTO.toEntity(SpecParam.class));
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PostMapping("/group")
    public ResponseEntity<Void> addSpecGroup(@RequestBody SpecGroupDTO specGroupDTO) {
        specGroupService.addSpecGroup(specGroupDTO.toEntity(SpecGroup.class));
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping("/group")
    public ResponseEntity<Void> updateGroup(@RequestBody SpecGroupDTO specGroupDTO) {
        specGroupService.updateGroup(specGroupDTO.toEntity(SpecGroup.class));
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @PutMapping("/param")
    public ResponseEntity<Void> updateParam(@RequestBody SpecParamDTO specParamDTO) {
        specParamService.updateParam(specParamDTO.toEntity(SpecParam.class));
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @DeleteMapping("/group/{id}")
    public ResponseEntity<Void> deleteGroupById(@PathVariable("id") Long id){
        specGroupService.deleteGroupById(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @DeleteMapping("/param/{id}")
    public ResponseEntity<Void> deleteParamById(@PathVariable("id") Long id){
        specParamService.deleteParamById(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
